import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * template - 模板model
 * @since 2019-1-28
 * @version 0.0.1

 */
import { getResponse, createPagination, parseParameters } from 'utils/utils';
import { queryMapIdpValue } from 'services/api';
import { query as _query, update as _update, create as _create, queryOneHeader as _queryOneHeader, removeHeader as _removeHeader, queryCode as _queryCode, fetchColumnList as _fetchColumnList, fetchColumnDetail as _fetchColumnDetail, deleteColumn as _deleteColumn, createColumn as _createColumn, updateColumn as _updateColumn } from "../services/templateService";
export default {
  namespace: 'template',
  state: {
    headerList: [],
    headerData: {},
    code: {},
    // 值集集合
    templateTargetList: [],
    // sheet 列表数据
    templateTargetPagination: {},
    // sheet 列表数据分页
    pagination: {},
    // 列表分页
    templateColumnDetail: {},
    // 模板详情
    templateColumnList: [],
    // 模板列数据
    columnPagination: {} // 模板列分页对象

  },
  effects: {
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_ref, _ref2) {
      var payload, call, put, lovCodes, res, typeList, sheetList;
      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              lovCodes = payload.lovCodes;
              _context.t0 = getResponse;
              _context.next = 6;
              return call(queryMapIdpValue, lovCodes);

            case 6:
              _context.t1 = _context.sent;
              res = (0, _context.t0)(_context.t1);
              typeList = res.typeList, sheetList = res.sheetList;
              _context.next = 11;
              return put({
                type: 'setCodeReducer',
                payload: {
                  'CIMP.TEMPLATE.TEMPLATETYPE': typeList,
                  'CIMP.IMPORT_SHEET': sheetList
                }
              });

            case 11:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    // 查询通用模板列表数据
    query: /*#__PURE__*/_regeneratorRuntime.mark(function query(_ref3, _ref4) {
      var payload, call, put, res;
      return _regeneratorRuntime.wrap(function query$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context2.next = 4;
              return call(_query, payload);

            case 4:
              res = _context2.sent;

              if (!getResponse(res)) {
                _context2.next = 8;
                break;
              }

              _context2.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  headerList: res.content,
                  pagination: createPagination(res)
                }
              });

            case 8:
              return _context2.abrupt("return", res);

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, query);
    }),
    queryOneHeader: /*#__PURE__*/_regeneratorRuntime.mark(function queryOneHeader(_ref5, _ref6) {
      var payload, call, put, res, _res$templateTargetLi, templateTargetList;

      return _regeneratorRuntime.wrap(function queryOneHeader$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call, put = _ref6.put;
              _context3.next = 4;
              return call(_queryOneHeader, payload);

            case 4:
              res = _context3.sent;

              if (!getResponse(res)) {
                _context3.next = 9;
                break;
              }

              _res$templateTargetLi = res.templateTargetList, templateTargetList = _res$templateTargetLi === void 0 ? [] : _res$templateTargetLi;
              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  headerData: res,
                  templateTargetList: templateTargetList,
                  templateTargetPagination: createPagination(templateTargetList)
                }
              });

            case 9:
              return _context3.abrupt("return", res);

            case 10:
            case "end":
              return _context3.stop();
          }
        }
      }, queryOneHeader);
    }),
    // 更新模板头数据
    update: /*#__PURE__*/_regeneratorRuntime.mark(function update(_ref7, _ref8) {
      var payload, call, put, res;
      return _regeneratorRuntime.wrap(function update$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call, put = _ref8.put;
              _context4.next = 4;
              return call(_update, payload);

            case 4:
              res = _context4.sent;

              if (!getResponse(res)) {
                _context4.next = 9;
                break;
              }

              _context4.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  headerData: res
                }
              });

            case 8:
              return _context4.abrupt("return", res);

            case 9:
            case "end":
              return _context4.stop();
          }
        }
      }, update);
    }),
    // 创建模板头数据
    create: /*#__PURE__*/_regeneratorRuntime.mark(function create(_ref9, _ref10) {
      var payload, call, put, res;
      return _regeneratorRuntime.wrap(function create$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call, put = _ref10.put;
              _context5.next = 4;
              return call(_create, payload);

            case 4:
              res = _context5.sent;

              if (!getResponse(res)) {
                _context5.next = 9;
                break;
              }

              _context5.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  headerData: res
                }
              });

            case 8:
              return _context5.abrupt("return", res);

            case 9:
            case "end":
              return _context5.stop();
          }
        }
      }, create);
    }),
    fetchColumnList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchColumnList(_ref11, _ref12) {
      var payload, call, put, res;
      return _regeneratorRuntime.wrap(function fetchColumnList$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call, put = _ref12.put;
              _context6.next = 4;
              return getResponse(call(_fetchColumnList, parseParameters(payload)));

            case 4:
              res = _context6.sent;

              if (!(res && res.content)) {
                _context6.next = 8;
                break;
              }

              _context6.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  templateColumnList: res.content,
                  columnPagination: createPagination(res)
                }
              });

            case 8:
              return _context6.abrupt("return", res);

            case 9:
            case "end":
              return _context6.stop();
          }
        }
      }, fetchColumnList);
    }),
    fetchColumnDetail: /*#__PURE__*/_regeneratorRuntime.mark(function fetchColumnDetail(_ref13, _ref14) {
      var payload, call, put, res;
      return _regeneratorRuntime.wrap(function fetchColumnDetail$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref13.payload;
              call = _ref14.call, put = _ref14.put;
              _context7.next = 4;
              return getResponse(call(_fetchColumnDetail, payload));

            case 4:
              res = _context7.sent;

              if (!res) {
                _context7.next = 8;
                break;
              }

              _context7.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  templateColumnDetail: res
                }
              });

            case 8:
              return _context7.abrupt("return", res);

            case 9:
            case "end":
              return _context7.stop();
          }
        }
      }, fetchColumnDetail);
    }),
    // 创建模板列数据
    createColumn: /*#__PURE__*/_regeneratorRuntime.mark(function createColumn(_ref15, _ref16) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createColumn$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref15.payload;
              call = _ref16.call;
              _context8.next = 4;
              return call(_createColumn, payload);

            case 4:
              res = _context8.sent;
              return _context8.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context8.stop();
          }
        }
      }, createColumn);
    }),
    // 更新模板列数据
    updateColumn: /*#__PURE__*/_regeneratorRuntime.mark(function updateColumn(_ref17, _ref18) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateColumn$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref17.payload;
              call = _ref18.call;
              _context9.next = 4;
              return call(_updateColumn, payload);

            case 4:
              res = _context9.sent;
              return _context9.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context9.stop();
          }
        }
      }, updateColumn);
    }),
    // 删除模板列数据
    deleteColumn: /*#__PURE__*/_regeneratorRuntime.mark(function deleteColumn(_ref19, _ref20) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function deleteColumn$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              payload = _ref19.payload;
              call = _ref20.call;
              _context10.next = 4;
              return call(_deleteColumn, payload);

            case 4:
              res = _context10.sent;
              return _context10.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context10.stop();
          }
        }
      }, deleteColumn);
    }),
    // 删除头数据
    removeHeader: /*#__PURE__*/_regeneratorRuntime.mark(function removeHeader(_ref21, _ref22) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function removeHeader$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              payload = _ref21.payload;
              call = _ref22.call;
              _context11.next = 4;
              return call(_removeHeader, payload);

            case 4:
              res = _context11.sent;
              return _context11.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context11.stop();
          }
        }
      }, removeHeader);
    }),
    // 查询值集
    queryCode: /*#__PURE__*/_regeneratorRuntime.mark(function queryCode(_ref23, _ref24) {
      var payload, put, call, response;
      return _regeneratorRuntime.wrap(function queryCode$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              payload = _ref23.payload;
              put = _ref24.put, call = _ref24.call;
              _context12.next = 4;
              return call(_queryCode, payload);

            case 4:
              response = _context12.sent;

              if (!(response && !response.failed)) {
                _context12.next = 8;
                break;
              }

              _context12.next = 8;
              return put({
                type: 'setCodeReducer',
                payload: _defineProperty({}, payload.lovCode, response)
              });

            case 8:
            case "end":
              return _context12.stop();
          }
        }
      }, queryCode);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref25) {
      var payload = _ref25.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    },
    setCodeReducer: function setCodeReducer(state, _ref26) {
      var payload = _ref26.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        code: Object.assign(state.code, payload)
      });
    }
  }
};